package com.hao.tool.common.util;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk.he
 * @date 2020-02-26 14:07
 */
public class HttpMotUtil {

    public static final String DOT_ZIP = ".zip";
    public static final SimpleDateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS");


    /**
     * 发送压缩包到某个点
     *
     * @param url      请求地址
     * @param zipFile  zip文件
     * @param fileName 文件名
     * @return
     * @throws IOException
     */
    public String sendMotZip(String url, File zipFile, String fileName) throws IOException {
        String filePr = "";
        if (!fileName.contains(DOT_ZIP)) {
            filePr = DOT_ZIP;
        }
        return sendMotZip(url, zipFile, fileName, filePr);
    }


    public String sendMotZip(String url, File zipFile, String fileName, String filePrefix) throws IOException {
        Map<String, String> headerMap = new HashMap<>();
        Map<String, String> params = new HashMap<>();
        Map<String, File> fileMap = new HashMap<>();
        String fileMd5 = DigestUtils.md5Hex(new FileInputStream(zipFile.getAbsolutePath()));
        headerMap.put("binfile-md5", fileMd5);
        headerMap.put("binfile-gzip", "true");
        headerMap.put("binfile-auth", "");
        params.put("filename", fileName + filePrefix);
        fileMap.put("binFile", zipFile);
        String response = null;
        try {
            response = HttpClientUtil.build().setHeaders(headerMap)
                    .sendPost(url, params, fileMap);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }


    /**
     * 发送请求到某个点
     *
     * @param url 请求地址
     * @return
     * @throws IOException
     */
    public File receiveMotZip(String url, String basePath) throws IOException {
        Map<String, String> headerMap = new HashMap<>();
        File zipFile = null;
        try {
            HttpGet httpBase = new HttpGet(url);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            CloseableHttpResponse httpResponse = httpClient.execute(httpBase);
            String fileName = "";
            String disposition = httpResponse.getFirstHeader("Content-Disposition").getValue();
            if (StringUtils.isNotBlank(disposition)) {
                fileName = disposition.substring(disposition.indexOf("filename=") + "filename=".length());
            }
            if (StringUtils.isNotBlank(fileName)) {
                zipFile = new File(basePath + File.separator + fileName);
            } else {
                zipFile = new File(basePath + File.separator + DEFAULT_FORMAT.format(new Date()));
            }
            if (httpResponse.getEntity().getContentLength() != 0) {
                FileUtil.writeFromStream(httpResponse.getEntity().getContent(), zipFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return zipFile;
    }


}
